package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.HotActivity;
import flc.ast.activity.ProMusicActivity;
import flc.ast.activity.ProSelVideoActivity;
import flc.ast.activity.SearchMovieActivity;
import flc.ast.activity.SelPictureActivity;
import flc.ast.adapter.HotAdapter;
import flc.ast.adapter.MovieKindAdapter;
import flc.ast.adapter.MyMovieBannerAdapter;
import flc.ast.bean.MyBannerBean;
import flc.ast.databinding.FragmentMovieBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkResourceBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MovieFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MovieFragment extends BaseNoModelFragment<FragmentMovieBinding> {

    private MovieKindAdapter movieKindAdapter;
    private int oldPosition = 0;
    private HotAdapter hotAdapter, mustSeeAdapter, likeAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_movie;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.movieBanner.setBannerGalleryEffect(30, 10);//40两边露出距离，20图片之间距离
        mDataBinding.movieBanner.isAutoLoop(true);//false不自动轮播
        mDataBinding.movieBanner.setIndicator(new CircleIndicator(getActivity()));
        mDataBinding.movieBanner.setIndicatorGravity(IndicatorConfig.Direction.CENTER);
        mDataBinding.rvMovieKindList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        movieKindAdapter = new MovieKindAdapter();
        mDataBinding.rvMovieKindList.setAdapter(movieKindAdapter);
        movieKindAdapter.setOnItemClickListener(this);
        //近期热门
        mDataBinding.rvHotList.setLayoutManager(new GridLayoutManager(mContext, 3));
        hotAdapter = new HotAdapter();
        mDataBinding.rvHotList.setAdapter(hotAdapter);
        hotAdapter.setOnItemClickListener(this);
        //必看榜单
        mDataBinding.rvMustSeeList.setLayoutManager(new GridLayoutManager(mContext, 3));
        mustSeeAdapter = new HotAdapter();
        mDataBinding.rvMustSeeList.setAdapter(mustSeeAdapter);
        mustSeeAdapter.setOnItemClickListener(this);
        //猜你喜欢
        mDataBinding.rvLikeList.setLayoutManager(new GridLayoutManager(mContext, 3));
        likeAdapter = new HotAdapter();
        mDataBinding.rvLikeList.setAdapter(likeAdapter);
        likeAdapter.setOnItemClickListener(this);
        mDataBinding.ivMovieSearch.setOnClickListener(this);
        mDataBinding.tvHotAll.setOnClickListener(this);
        mDataBinding.tvLikeAll.setOnClickListener(this);
        mDataBinding.tvMustSeeAll.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivMovieSearch:
                String movieKey = mDataBinding.etMovieKey.getText().toString().trim();
                if (movieKey.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_search));
                    return;
                }
                SearchMovieActivity.movieKey = movieKey;
                startActivity(SearchMovieActivity.class);
                break;
            case R.id.tvHotAll://近期热门
                HotActivity.type = 0;
                startActivity(HotActivity.class);
                break;
            case R.id.tvMustSeeAll://必看榜单
                HotActivity.type = 1;
                startActivity(HotActivity.class);
                break;
            case R.id.tvLikeAll://猜你喜欢
                HotActivity.type = 2;
                startActivity(HotActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == movieKindAdapter) {
            movieKindAdapter.getItem(oldPosition).setSelected(false);
            movieKindAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            movieKindAdapter.getItem(position).setSelected(true);
            movieKindAdapter.notifyItemChanged(position);
            List<MyBannerBean> listBanner = new ArrayList<>();
            for (StkResBean stkResBean : movieKindAdapter.getItem(position).getDataList()) {
                listBanner.add(new MyBannerBean(stkResBean.getThumbUrl(), stkResBean.getName(),stkResBean.getUrl()));
            }
            MyMovieBannerAdapter myMovieBannerAdapter = new MyMovieBannerAdapter(listBanner, mContext);
            mDataBinding.movieBanner.setAdapter(myMovieBannerAdapter, true);//false不无限循环
            mDataBinding.movieBanner.setIndicatorSelectedColor(Color.parseColor("#FFB999FE"));
            mDataBinding.movieBanner.setIndicatorNormalColor(Color.parseColor("#1A081935"));
            myMovieBannerAdapter.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(Object data, int position) {
                    MyBannerBean bean = myMovieBannerAdapter.getData(position);
                    BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getText());
                }
            });
        } else {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
        }
    }

    @Override
    protected void initData() {
        getBannerData();
        getHotData();
        getMustSeeData();
        getLikeData();
    }

    private void getBannerData() {
        StkResApi.getChildTagAndResource(null, AppConfig.BANNER_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        Collections.reverse(result);
                        movieKindAdapter.setList(result);
                        movieKindAdapter.getItem(0).setSelected(true);
                        movieKindAdapter.notifyItemChanged(0);
                        List<MyBannerBean> listBanner = new ArrayList<>();
                        for (StkResBean stkResBean : movieKindAdapter.getItem(0).getDataList()) {
                            listBanner.add(new MyBannerBean(stkResBean.getThumbUrl(), stkResBean.getName(), stkResBean.getUrl()));
                        }
                        MyMovieBannerAdapter myMovieBannerAdapter = new MyMovieBannerAdapter(listBanner, mContext);
                        mDataBinding.movieBanner.setAdapter(myMovieBannerAdapter, true);//false不无限循环
                        mDataBinding.movieBanner.setIndicatorSelectedColor(Color.parseColor("#FFB999FE"));
                        mDataBinding.movieBanner.setIndicatorNormalColor(Color.parseColor("#1A081935"));
                        myMovieBannerAdapter.setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(Object data, int position) {
                                MyBannerBean bean = myMovieBannerAdapter.getData(position);
                                BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getText());
                            }
                        });
                    }
                }
            }
        });
    }

    private void getHotData() {
        StkResApi.getTagResourceList(null, AppConfig.HOT_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        hotAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getMustSeeData() {
        StkResApi.getTagResourceList(null, AppConfig.MUST_SEE_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        mustSeeAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getLikeData() {
        StkResApi.getTagResourceList(null, AppConfig.LIKE_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        likeAdapter.setList(result);
                    }
                }
            }
        });
    }

}